package com.scu.phototabuser.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.scu.phototabuser.R;

import static android.text.InputType.TYPE_CLASS_TEXT;
import static android.text.InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS;
import static android.text.InputType.TYPE_TEXT_VARIATION_PASSWORD;

/**
 * Created by tianfei on 2017/4/29.
 *
 *
 * inputType 接受两种参数： email 和 password
 *
 *
 */

public class EditTextWithImgLeft extends LinearLayout {


    private ImageView imgView;
    private EditText editText;

    private String _text;
    private String _inputType;
    private String _hint;
    private int _resID;


    public EditTextWithImgLeft(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context,attrs);
    }

    public void setText(String t){
        editText.setText(t);
    }

    public void setError(String e){
        editText.setError(e);
    }

    public String getText(){
        return editText.getText().toString();
    }

    private void initAttrs(Context context,AttributeSet attrs){

        this.setOrientation(HORIZONTAL);
        //加载布局
        LayoutInflater.from(context).inflate(R.layout.composite_img_edtext, this, true);
        imgView=(ImageView)findViewById(R.id.composite_img);
        editText=(EditText)findViewById(R.id.composite_etx);

        //从xml的属性中获取到字体颜色与string
        TypedArray t=context.obtainStyledAttributes(attrs, R.styleable.ImageEditTextStyle);

        _text=t.getString(R.styleable.ImageEditTextStyle_rightText);
        //t.getDrawable(R.styleable.ImageEditTextStyle_leftImage);
        _resID=t.getResourceId(R.styleable.ImageEditTextStyle_leftImage,0);
        _inputType=t.getString(R.styleable.ImageEditTextStyle_inputTextType);
        _hint=t.getString(R.styleable.ImageEditTextStyle_hint);
        t.recycle();




        //设置xml中的值到控件，同时设定属性，暂时不支持自定义其他属性
        imgView.setBackgroundResource(_resID);
        editText.setText(_text);
        editText.setTextColor(Color.WHITE);
        editText.setHintTextColor(Color.parseColor("#FFF100"));
        editText.setHint(_hint);


        if ("email".equals(_inputType)){
            editText.setInputType(TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
        }else
        if ("password".equals(_inputType)){
            editText.setInputType(TYPE_CLASS_TEXT|TYPE_TEXT_VARIATION_PASSWORD);
        }

    }
}
